package com.ereach;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DemoArrayList {
    public static void main(String[] args) {
        System.out.println("insert elements to a list");
        List<String> list1 = new ArrayList<>();

        list1.add("Lion");
        list1.add("Tiger");
        list1.add("Cat");

        System.out.println(list1);

        System.out.println("iterate 1");
        for (var x : list1)
            System.out.println(x);

        System.out.println("iterate 2");
        list1.forEach(x -> System.out.println(x));

        System.out.println("iterate 3");
        Iterator<String> iterator = list1.iterator();
        while (iterator.hasNext()) {
            String item = iterator.next();
            System.out.println(item);
        }

        System.out.println("insert dog to list specified position");
        list1.add(1, "Dog");
        System.out.println(list1);

        System.out.println("create list from existing collection");
        List<String> list2 = new ArrayList<>(list1);
        list2.add("Bird");
        list2.add("Fish");
        System.out.println(list2);

        System.out.println("access list from specified position");
        System.out.println(list2.get(2));
        System.out.println(list2.get(4));

        System.out.println("remove element from list by index");
        list2.remove(3);
        System.out.println(list2);

        System.out.println("remove element by its name");
        list2.remove("Bird");
        System.out.println(list2);

        System.out.println("remove all elements from a list");
        list2.removeAll(list2);
        System.out.println(list2);;
    }
}
